/*
 * @Author: 彭林
 * @Date: 2021-06-29 15:05:44
 * @LastEditTime: 2021-07-01 15:40:25
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/student_card.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"
	"strconv"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetStudentCardList(r *ghttp.Request) {
	student_id := r.GetQueryInt("student_id")
	school_id := r.GetQueryInt("school_id")
	campus_id := r.GetQueryInt("campus_id")
	status := r.GetQuery("status")

	if status == nil {
		status = -1
	}

	result := service.StudentCardService().GetStudentCardList(school_id, campus_id, gconv.Int(status), student_id)
	response.JsonExit(r, 0, "ok", result)
}

func CreateStudentCard(r *ghttp.Request) {

	var (
		data        *define.StudentCardRequest
		studentCard *model.StudentCard
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &studentCard); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	studentCard.Status = strconv.Itoa(int(data.IsAutoActive))
	studentCard.Total = data.NumSum
	studentCard.Residue = data.NumSum

	result := service.StudentCardService().CreateStudentCard(studentCard)
	response.JsonExit(r, 0, "ok", result)
}
